package org.wk.user.api.controller;

import com.alibaba.fastjson.JSONObject;
import com.google.common.base.Strings;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.wk.core.common.view.ResponseVo;
import org.wk.user.service.PromoteService;
import org.wk.user.view.UserVo;

@Controller
@RequestMapping("users/promote")
public class PromoteController {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private PromoteService promoteService;

    /**
     * 获取分享信息
     */
    @RequestMapping(value="shareinfo",method=RequestMethod.GET,produces=MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    public String shareInfo() {
        logger.info("获取用户分享信息>>>>{}");
        ResponseVo responseVo = promoteService.shareInfo();
        logger.info("获取用户分享信息，返回结果>>>>{}",responseVo.toString());
        return responseVo.toString();

    }

    @RequestMapping(value="register",method=RequestMethod.GET)
    public String toRegisterPage() {
        logger.info("跳转到推广注册页面>>>>{}");
        return "redirect:/users/promote/referral_code";
    }

    /**
     * 跳转到推广注册页面
     */
    @RequestMapping(value="referral_code",method=RequestMethod.GET)
    public String toRegisterReferralPage(@RequestParam(value = "referralCode", required = false) String referralCode,ModelMap modelMap) {
        logger.info("跳转到推广注册页面>>>>{}");
        referralCode = referralCode==null?"":referralCode;
        modelMap.put("referralCode",referralCode);
        logger.info("获取用户分享信息，返回结果>>>>{}");
        return "promote/register";

    }
    /**
     * 注册，成功后跳转到成功页面
     * @param userVo
     * @return
     */
    @RequestMapping(value="register",method=RequestMethod.POST)
    public String register(UserVo userVo) {
        logger.info("推广注册>>>>{}",JSONObject.toJSONString(userVo));
        ResponseVo responseVo = promoteService.shareInfo();
        logger.info("获取用户分享信息，返回结果>>>>{}",responseVo.toString());
        return responseVo.toString();

    }


}
